package com.xuetang9.t9.hotel.mapper.sql.impl;

import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.condition.impl.MaintainRoomCondition;
import com.xuetang9.t9.hotel.mapper.sql.IMapperSql;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @author Pang
 */
public class MaintainRoomMapperSql implements IMapperSql {

    @Override
    public String selectByAll(Map map){
        ICondition key = (ICondition) map.get("key");
        return getCondition(key,"*",true);
    }

    @Override
    public String selectByCount(Map map){
        ICondition key = (ICondition) map.get("key");
        return getCondition(key,"COUNT(*)",false);
    }


    private String getCondition(ICondition condition,String select,boolean isPage) {
        SQL sql = new SQL();
        sql.SELECT(select).FROM("g_maintainroom");
        if(condition == null){
            return sql.toString();
        }
        MaintainRoomCondition key = null;
        if(condition instanceof MaintainRoomCondition){
            key = (MaintainRoomCondition)condition;
        }else{
            throw  new RuntimeException("condition type Don't match");
        }
        if(null != key){
            if(null != key.getRoomId() && !key.getRoomId().isEmpty()){
                sql.WHERE("room_id IN ((SELECT room_id FROM g_roominfo WHERE room_num LIKE '% "+key.getRoomId()+"%'))");
            }
            if(null != key.getName() && !key.getName().isEmpty()){
                sql.WHERE("staff_id IN ((SELECT staff_id FROM g_staffinfo WHERE staff_name LIKE '%"+key.getName()+"%'))");
            }
            if(null != key.getStateId() && key.getStateId() >= 0){
                sql.WHERE("maintain_state = "+key.getStateId());
            }
            if(null != key.getType() && key.getType() >= 0){
                sql.WHERE("maintain_type = "+key.getType());
            }
            if(key.judge()  && isPage){
                sql.LIMIT(key.getCurrPage()+","+key.getPageSize());
            }
        }
        return sql.toString();
    }


}
